﻿using System.Collections.Generic;
using System.Text;
using System.Web;
using Obsidian.Mvc;
using Obsidian.Utils;
using Obsidian.Web;
using [#ApiNS#]

/*============================
 * @author [#AUTHOR#]
 * @created [#CREATED#]
 * @desc 
* ============================ */

namespace [#ctrlNS#]
{
    public class [#ClsName#]Ctrl : [#ClsBaseCtrl#]
    {
		/// <summary>
        /// 首页视图
        /// </summary>
        /// <param name="context"></param>
        public void Index(HttpContext context)
        {
			OView vIndex = new OView("[#ClsName#]/Index");
			vIndex.Display();
        }

		/// <summary>
        /// 列表页视图
        /// </summary>
        /// <param name="context"></param>
        public void List(HttpContext context)
        {
            Entity e[#ClsName#]Qry = new Entity();

            #region [构建查询条件]

			//构造分页条件
            int pageNo;
            if (!ParamsReceiver.TryGetQueryInt32("pageNo", out pageNo))
            {
                pageNo = 1;
            }
            e[#ClsName#]Qry.Set("pageNo", pageNo);
            e[#ClsName#]Qry.Set("pageSize", 10);	//默认分页大小

			//构造查询条件


			//构造排序条件
            string orderBy;
            if (ParamsReceiver.TryGetString("orderBy", out orderBy)
                && !string.IsNullOrWhiteSpace(orderBy))
            {
                orderBy = orderBy.ToString();
				e[#ClsName#]Qry.Set("orderBy", "created:desc");
            }

            #endregion [构建查询条件]

            [#ClsName#]API [#sClsName#]Api = new [#ClsName#]API();
            this.PrepareApi([#sClsName#]Api, this);
            
            apiRes = [#sClsName#]Api.List(e[#ClsName#]Qry);
            EntitySet es[#ClsName#]s = apiRes.GetEntitySet("[#sClsName#]s");
            Entity eListAttr = apiRes.GetEntity("listAttr");	//分页属性实体

            int pageCount = eListAttr.GetInt("pagesCount");
            if (pageNo > pageCount || pageNo <=0)
            {
                this.Error(404);
                return;
            }

            //构建视图
            OView vList = new OView("[#ClsName#]/List");
            vList.SetData("es[#ClsName#]s", es[#ClsName#]s);
            vList.SetData("esListAttr", eListAttr);
            vList.SetData("es[#ClsName#]Qry", e[#ClsName#]Qry);

			//构建顶部视图
			//this.CreateTopBarView(vList);

			//构建头部和底部视图
            //this.CreateHeaderAndFooterView(vList, HeadActiveButton.[#ClsName#]);

			//呈现视图
            vList.Display();
        }

		/// <summary>
        /// 详情页视图
        /// </summary>
        /// <param name="context"></param>
        public void Detail(HttpContext context)
        {
            [#IdSysType#] [#IdAlias#];
            if (!ParamsReceiver.[#TryGetIdType#]("[#IdAlias#]", out [#IdAlias#]))
            {
                this.Error(404);
                return;
            }

            [#ClsName#]API [#sClsName#]Api = new [#ClsName#]API();
            this.PrepareApi([#sClsName#]Api);
            ApiResponse apiRes = [#sClsName#]Api.Browse([#IdAlias#]);

            Entity qry = new Entity();
            Entity e[#ClsName#] = apiRes.GetEntity("[#sClsName#]");

            OView vDetail = new OView("[#ClsName#]/Detail");
            vDetail.SetData("e[#ClsName#]", e[#ClsName#]);

			//构建顶部视图
            //this.CreateTopBarView(vDetail);

			//构建头部和底部视图
            //this.CreateHeaderAndFooterView(vDetail, HeadActiveButton.[#ClsName#]);

			//构建百度分享视图
            //this.CreateBaiduShareView(vDetail);

            vDetail.Display();
        }

		/// <summary>
        /// 创建列表URL
        /// </summary>
        /// <param name="qry"></param>
        public static string Create[#ClsName#]ListUrl(Entity qry)
        {
            string url = "列表URL.htm";		//根据AppConfig的<urlRewrite>配置自行修改
            Dictionary<string, object> dict = qry.ToDictionary(
                "pageNo,orderBy", true);	//根据请求参数自行修改
            string listUrl = WebUtil.CreateUrl(url, dict).ToLower();
            return listUrl;
        }

		/// <summary>
        /// 创建明细URL
        /// </summary>
        /// <param name="[#IdAlias#]"></param>
        public static string Create[#ClsName#]DetailUrl(string [#IdAlias#])
        {
			string detailUrl = "";
            StringBuilder sb = new StringBuilder();
            sb.Append("明细标识-")	//标识；根据AppConfig的<urlRewrite>配置自行修改
			.Append("-")			//分隔符
			.Append([#IdAlias#])	//ID
			.Append(".htm");		//后缀
			detailUrl = sb.ToString().ToLower();
            return detailUrl;
        }
    }
}